package com.liang.study.design.service.engine;

import com.liang.study.design.constant.RuleEnum;
import com.liang.study.design.model.combin.TreeRich;
import com.liang.study.design.model.vo.EngineResult;
import com.liang.study.design.model.vo.TreeNode;
import com.liang.study.design.model.vo.TreeNodeLink;
import com.liang.study.design.model.vo.TreeRoot;
import com.liang.study.design.service.logic.LogicFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

import static com.liang.study.design.service.engine.EngineConfig.logicFilterMap;

/**
 * @author lsm
 * @ClassName EngineBae.java
 * @Description TODO
 * @createTime 2023年12月21日 10:54:00
 */
public abstract class EngineBae implements IEngine{

    Logger logger = LoggerFactory.getLogger("EngineBae");

    @Override
    public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);


    //从根节点找果实
    protected TreeNode engineDecisionMaker(TreeRich treeRich,  Map<String, String> decisionMatter) {
        TreeRoot treeRoot = treeRich.getTreeRoot();
        Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();

        Long id = treeRoot.getId();
        TreeNode treeNode = treeNodeMap.get(id);

        while (treeNode.getNodeType()==1){
            RuleEnum ruleEnum = treeNode.getRuleEnum();
            LogicFilter logicFilter = logicFilterMap.get(ruleEnum);
            String matterValue = logicFilter.getValueFromMap(decisionMatter);
            Long ruleKey = logicFilter.logicFilter(matterValue, treeNode.getLinkList());

            treeNode = treeNodeMap.get(ruleKey);
            logger.info("决策树引擎=>{}  treeNode： {} ruleKey： {} matterValue： {}",
                    treeRoot.getTreeName(), treeNode.getTreeNodeId(), ruleKey, matterValue);
        }
        return treeNode;
    }
}
